package com.example.administrator.demob;

import android.graphics.Bitmap;
import android.util.Log;
import android.util.LruCache;

import com.android.volley.toolbox.ImageLoader;

class MyImageCache implements ImageLoader.ImageCache{
    private final String TAG = "MyImageCache";
    //Lru算法：最少最近使用
    private LruCache<String, Bitmap> lruCache;

    public MyImageCache(){
        int maxSize = 10 * 1024 * 1024;//10M
        lruCache = new LruCache<String, Bitmap>(maxSize){
            @Override
            protected int sizeOf(String key, Bitmap value) {
                return value.getRowBytes() * value.getHeight();//返回照片所需存储的大小
            }
        };
    }

    @Override
    public Bitmap getBitmap(String s) {
        Bitmap bitmap = lruCache.get(s);
        if(bitmap == null) {
            Log.e(TAG, "getBitmap failed bitmap == null");
        }else {
            Log.e(TAG, "getBitmap success bitmap != null");
        }
        return bitmap;
    }

    @Override
    public void putBitmap(String s, Bitmap bitmap) {
        lruCache.put(s, bitmap);
        if(bitmap == null) {
            Log.e(TAG, "putBitmap failed bitmap == null");
        }else {
            Log.e(TAG, "putBitmap success bitmap != null");
        }
    }
}
